package com.xiaoxin.experience.service.tree.storage;

import com.xiaoxin.experience.common.BaseSystemMessage;
import com.xiaoxin.experience.service.storage.JsonFileHandler;
import com.xiaoxin.experience.service.tree.model.Api;
import com.xiaoxin.experience.service.tree.model.ApiDirectory;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaoxin
 */
public class ApiJsonFileHandler
{

    private static final String API_DIRECTORY_FILE_PATH = BaseSystemMessage.baseCachePath() + "ApiDirectory.json";

    private static final String API_FILE_PATH = BaseSystemMessage.baseCachePath() + "Api.json";

    private static List<ApiDirectory> apiDirectories;

    private static List<Api> apis;

    static
    {
        init();
    }

    private static void init()
    {
        if (fileExists())
        {
            loadApiDirectory();
            loadApi();
            return;
        }
        initData();
    }

    private static void initData()
    {
        apiDirectories = new ArrayList<>();
        ApiDirectory root = new ApiDirectory("0","Collections","-1","0,");
        apiDirectories.add(root);
        apis = new ArrayList<>();
        saveApiDirectory();
        saveApi();
    }

    private static boolean fileExists()
    {
        File apiDirectoryFile = new File(API_DIRECTORY_FILE_PATH);
        File apiFile = new File(API_FILE_PATH);
        return apiDirectoryFile.exists() && apiFile.exists();
    }

    private static void loadApiDirectory()
    {
        apiDirectories = JsonFileHandler.loadJson(API_DIRECTORY_FILE_PATH, ApiDirectory.class);
    }

    private static void loadApi()
    {
        apis = JsonFileHandler.loadJson(API_FILE_PATH,Api.class);
    }

    public static List<ApiDirectory> getApiDirectories()
    {
        return apiDirectories;
    }

    public static List<Api> getApis()
    {
        return apis;
    }

    public static void saveApiDirectory()
    {
        JsonFileHandler.save(API_DIRECTORY_FILE_PATH,apiDirectories);
    }

    public static void saveApi()
    {
        JsonFileHandler.save(API_FILE_PATH,apis);
    }

    public static String generateApiId()
    {
        int id = apis.stream().map(api -> Integer.parseInt(api.getId())).max(Comparator.comparingInt(a -> a)).orElse(1) + 1;
        return String.valueOf(id);
    }

    public static String generateDirId()
    {
        int id = apiDirectories.stream().map(dir -> Integer.parseInt(dir.getId())).max(Comparator.comparingInt(a -> a)).orElse(1) + 1;
        return String.valueOf(id);
    }

    public static boolean apiExists(Api api)
    {
        if (apis.isEmpty())
        {
            return false;
        }
        return apis.stream().anyMatch(a -> a.getId().equals(api.getId()));
    }

    public static boolean ApiDirExists(ApiDirectory apiDirectory)
    {
        if (apiDirectories.isEmpty())
        {
            return false;
        }
        return apiDirectories.stream().anyMatch(dir -> dir.getId().equals(apiDirectory.getId()));
    }

    public static void deleteApi(String id)
    {
        apis = apis.stream().filter(api -> !api.getId().equals(id)).collect(Collectors.toList());
        saveApi();
    }

    public static boolean deleteApiDirectory(String id)
    {
        boolean hasSubDir = apiDirectories.stream().anyMatch(dir -> id.equals(dir.getParentId()));
        if (hasSubDir)
        {
            return false;
        }
        boolean hasApi = apis.stream().anyMatch(api -> id.equals(api.getDirectoryId()));
        if (hasApi)
        {
            return false;
        }
        apiDirectories = apiDirectories.stream().filter(dir -> !id.equals(dir.getId())).collect(Collectors.toList());
        saveApiDirectory();
        return true;
    }
}
